#!/usr/bin/python
# -*- coding: utf-8 -*-
import os,glob,imp,xbmc,re,urllib,urllib2,cookielib
modules = {}
folder=xbmc.translatePath(os.path.join(os.path.normpath(os.path.join(os.getcwd(),os.pardir,os.pardir)),'hoster'+os.sep+'oracle2'+os.sep))
#folder=xbmc.translatePath('Q:\\plugins\\hoster\\oracle2\\')
for bp in os.listdir(folder):
	path=folder+bp
	name, ext = os.path.splitext(bp)
	if name[0]!='_' and ext==".py" and os.path.isfile(path):
		modules[name] = imp.load_source(name, bp, open(path,'r'))


#--------------------------------------------------------------------------------------------------
#author:
#elypter
#-mail:elypter@yahoo.de
#
#for details look at readme.txt


#--------------------------------------------------------------------------------------------------
#debuggin: -1:Silent 0:No debugging 1:Normal 2:Everything
debug=1
dstr="##*oracle*oracle2*"

if debug > 0: print dstr+"modules: "+ str(modules)

def get_verion():
	#or look at the filename
	return 2
	
#--------------------------------------------------------------------------------------------------
def get_http(url):
	BASE_PATH = os.getcwd().replace( ";", "" )
	BASE_DATA_PATH = os.path.join( xbmc.translatePath("special://profile/plugin_data/hoster"), "oracle" )
	
	cookiefile = os.path.join(BASE_DATA_PATH,'oracle.lwp')
	cj = cookielib.LWPCookieJar()
	if os.path.isfile(cookiefile):
		cj.load(cookiefile)
	opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
	urllib2.install_opener(opener)
	#cj.clear()
	
	req = urllib2.Request(url)
	req.add_header('Referer',url)
	req.add_header('Host',re.compile('^http\://([^/]*)/.*').findall(url)[0] )
	req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5')
	req.add_header('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
	req.add_header('Accept-Language', 'de-de,de;q=0.5')
	#req.add_header('Accept-Encoding', 'gzip,deflate')
	req.add_header('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7')
	req.add_header('Keep-Alive', '300')
	req.add_header('Connection', 'keep-alive')
	req.add_header('Cache-Control', 'max-age=0')

	return req

def resolve(url):
	response = urllib2.urlopen(get_http(url))
	return response.geturl()

def get_page(url):
	response = urllib2.urlopen(get_http(url))
	page=response.read()
	response.close()
	page=page.replace('\0','')
	return page

		
#--------------------------------------------------------------------------------------------------	
def get_link_by_url(url,options):
	return get_links_by_url(url,options)[0]
	#return #status,link,image,title,filename,captcha,hoster	#return item1

def get_link_by_verification(url,code,options):
	return get_links_by_verification(url,code,options)[0]
	
#def get_links_by_url(url,options):
def get_links_by_source(url,options):
	hosters=[]
	mirrs=[]
	
	#if debug > 0: print dstr+"get_links_source* url: "+url+" options: "+str(options)
	
	
	for key,module in modules.items():
		hoster=module.get_name()
		regexp=module.get_regexp()
		if debug > 1: print dstr+"get_links_source* executing regexp: hoster= "+hoster+" regexp= "+regexp
		links=re.compile(regexp).findall(url)
		links=list(set(links))
		if links != None and len(links)>0:
			for link in links:
				mlink=module.join(link)
				if mlink != None and len(mlink)>0:
					#h,link,i=module.get_link1(mlink)
					status1,link1,image1,title1,filename1,captcha1,hoster1=module.get_link1(mlink)
				if status1=="got":
					mirr=("got",link1,'',hoster+' ('+' )','','',hoster)
				elif status1=="captcha":
					mirr=("captcha",link1,'',hoster+' ('+' )','',captcha1,hoster)
				else:
					mirr=(status1,link1,'',hoster+' ('+' )','','',hoster)
				mirrs.append(mirr)
				if debug > 0: print dstr+"get_links_source* mirror added: hoster= "+hoster+" mirror= "+str(mirr)
			break#muss evtl noch geaendert werden		
				
	return mirrs

def get_links_by_verification(url,code,options):
	if debug > 0: print dstr+"get_links_by_verification* verification for{ url: "+url+" code: "+code+" options: "+str(options)
	status2,link2,image2,title2,filename2,captcha2,hoster2="","","","","","",""
	for key,module in modules.items():
		hoster=module.get_name()
		regexp=module.get_regexp()
		links=re.compile(regexp).findall(url)
		if links != None and len(links)>0:
			mlink=module.join(links[0])
			if mlink != None and len(mlink)>0:
				#h,link,i=module.get_link1(mlink)
				status2,link2,image2,title2,filename2,captcha2,hoster2=module.get_link2(url,code)
				break
	if status2=="got":
		return (status2,link2,image2,title2,filename2,captcha2,hoster)
	else:#noch keine unterscheindung
		return (status2,link2,image2,title2,filename2,captcha2,hoster)
		
#def get_links_by_page_source(source,options):
#	return get_links_by_url(source,options)
#	
#	#return #array of return item1

#def get_links_by_page_url(url,options):
def get_links_by_url(url,options):
	return get_links_by_source(get_page(url),options)
	

def get_historys():#optional: hoster
	return #array of return item1
	
def do_history():#return item1 
	return #status
	
def del_history():#return item1
	return #status

def del_historys():
	return #status
	
	
def get_downloads():#optional: hoster
	return #array of return item1

def do_download():#return item1
	return #return item1||handle
	
def del_download():#return item1
	return #status

def del_downloads():
	return #status
	

def get_connected_by_url(url,options):
	#zB: verlinkte videos bei youtube
	return #array of conneced type (eg: playlist, related, other videos from user) each containing an array of (url,title,filename,hoster)	#return item2

def get_connected_by_verification(url,options):
	return #return item2
	
def get_connectedlink_by_url(url,options):
	return #array of conneced type (eg: playlist, related, other videos from user) each containing an array of return item1	#return item3

def get_connectedlink_by_verification(url,options):
	return #return item3